﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Application.Common.Utils;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageUserTable.Command
{
    public class EditUserTableCommand : IRequest<Json<string>>
    {
        public int id { get; set; }
        /// <summary>
        /// 客户名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        public string account { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string password { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string address { get; set; }
        public List<int> list_deviceid { get; set; }
    }

    public class EditUserTableCommandHandler : IRequestHandler<EditUserTableCommand, Json<string>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IUsertableRepository _iusertableRepository;
        private readonly IRoletableRepository _iroletableRepository;
        private readonly IUserdevicetableRepository _iuserdevicetableRepository;
        private readonly IDevicetableRepository _idevicetableRepository;
        public EditUserTableCommandHandler(ICurrentUserService icurrentUserServices, IUsertableRepository iusertableRepository, IRoletableRepository iroletableRepository, IUserdevicetableRepository iuserdevicetableRepository, IDevicetableRepository idevicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iusertableRepository = iusertableRepository;
            _iroletableRepository = iroletableRepository;
            _iuserdevicetableRepository = iuserdevicetableRepository;
            _idevicetableRepository = idevicetableRepository;
        }
        public async Task<Json<string>> Handle(EditUserTableCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            int roleid = _icurrentUserServices.GetRoleId();
            int roleType = _icurrentUserServices.GetRoletype();
            if (request.id <= 0)
            {
                return Json<string>.Failed("请选择需要修改的员工");
            }
            if (string.IsNullOrWhiteSpace(request.name))
            {
                return Json<string>.Failed("请输入员工姓名");
            }
            else if (request.name.Length > 50)
            {
                return Json<string>.Failed("员工姓名不能超过50个字");
            }
            else if (string.IsNullOrWhiteSpace(request.account))
            {
                return Json<string>.Failed("请输入账号");
            }
            else if ( request.account.Length < 8)
            {
                return Json<string>.Failed("账号至少输入8位");
            }
            else if (request.account.Length > 20)
            {
                return Json<string>.Failed("账号至多输入20位");
            }
            else if (!string.IsNullOrWhiteSpace(request.password) &&  request.password.Length < 8)
            {
                return Json<string>.Failed("密码至少输入8位");
            }
            else if (!string.IsNullOrWhiteSpace(request.password) && request.password.Length > 20)
            {
                return Json<string>.Failed("密码至多输入20位");
            }
            else if (!RegexHelper.RegexAccount(request.account))
            {
                return Json<string>.Failed("账号由数字或字母组成");
            }
            else if (!string.IsNullOrWhiteSpace(request.password) &&  !RegexHelper.RegexPassword(request.password))
            {
                return Json<string>.Failed("密码由字母和数字组成");
            }
            else if ((await _iusertableRepository.GetAccountCount(request.id,request.account, cancellationToken)) > 0)
            {
                return Json<string>.Failed("当前账号已经存在");
            }
            else
            {



                usertable model = await _iusertableRepository.GetUpdateUserTableById(request.id, userid, cancellationToken);
                if (!(model != null && model.id > 0))
                {
                    return Json<string>.Failed("暂无权限修改当前账号");
                }

                bool isUpdatePwd = false;

                model.name = request.name;
                model.account = request.account;
                model.address = request.address;
                if (!string.IsNullOrWhiteSpace(request.password))
                {
                    model.password = MD5Encrypt.GetMd5Hash(request.password);
                    isUpdatePwd = true;
                }

                model.updatetime = DateTime.Now;
                model.userpid = userid;

                await _iusertableRepository.EditDeviceTable(model, isUpdatePwd, cancellationToken);
                if (roleType==2)
                {
                    if (request.list_deviceid != null && request.list_deviceid.Any())
                    {
                        var list_device = await _idevicetableRepository.GetDevicetableListByIds(request.list_deviceid, userid, cancellationToken);
                        if (list_device != null && list_device.Any())
                        {
                            await _iuserdevicetableRepository.EditUserDevices(userid, model.id, list_device, cancellationToken);
                        }
                    }
                }
                return Json<string>.Success("修改成功");
            }
        }
    }
}
